<template>
  <div class="user" style="padding: 0px;">
    <el-card>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="部门">
          <el-select clearable style="width: 150px;" v-model="formInline.deptId" placeholder="请选择部门" :multiple="false" filterable @change="onSubmit">
            <el-option v-for="item in interDeparts" :value="item.value" :key="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名或姓名">
          <el-input v-model="formInline.name" placeholder="请输入用户名或姓名" @keyup.enter.native="onSubmit"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="formInline.phone" placeholder="请输入手机号" @keyup.enter.native="onSubmit"></el-input>
        </el-form-item>
        <el-form-item label="所属角色" prop="roleId">
          <el-select style="width: 150px;" clearable v-model="formInline.roleId" filterable @change="onSubmit">
            <el-option v-for="item in roleList" @change="onSubmit" :value="item.roleId" :key="item.roleId" :label="item.roleName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="onSubmit">查询</el-button>
          <el-button icon="Refresh" @click="reSet">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!--内容-->
    <el-card style="margin-top:5px">
      <el-button size="small" icon="Plus" @click="addClick" v-auth="'/auth/user/add'" plain>添加用户</el-button>
      <el-table size="small" :data="tableData" border style="width: 100%;height: 100%;margin-top:5px;" v-loading="loading" stripe>
        <el-table-column prop="shopUserId" label="用户ID" width="100" fixed align="center"></el-table-column>
        <el-table-column prop="userName" label="用户名" min-width="100" fixed></el-table-column>
        <el-table-column prop="realName" label="姓名" min-width="100"></el-table-column>
        <el-table-column label="所属角色" width="200">
          <template #default="scope">
            <div v-if="scope.row.isSuper == 0">
              <span class="mr10 blue" v-for="(item, index) in scope.row.roleList || []" :key="index">{{ item.roleName || '' }}</span>
            </div>
            <div class="gray" v-if="scope.row.isSuper">超级管理员</div>
          </template>
        </el-table-column>
        <el-table-column prop="userGender" label="性别" width="50" align="center">
          <template #default="scope">
            <div v-if="scope.row.userGender == 1">男</div>
            <div v-if="scope.row.userGender == 2">女</div>
          </template>
        </el-table-column>
        <el-table-column prop="userGroup" label="所属部门" width="150" align="center">
          <template #default="scope">
            <div v-if="scope.row.userGroup">
              <el-select v-model="scope.row.userGroup" disabled :multiple="false">
                <el-option v-for="item in interDeparts" :value="item.value" :key="item.value"
                  :label="item.label"></el-option>
              </el-select>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="userMobile" label="电话" width="150" align="center"></el-table-column>
        <!-- <el-table-column prop="areaIds" label="城市" width="300">
          <template #default="scope">
            <div v-html="renderOption(scope.row.areaList)"></div>
          </template>
        </el-table-column> -->
        <el-table-column prop="disabled" label="状态" align="center">
          <template #default="scope">
            <!-- <el-tag :type="scope.row.disabled ? 'danger' : 'success'">
              {{ scope.row.disabled ? '禁用' : '启用' }}
            </el-tag> -->
            <el-switch v-model="scope.row.active" @click="changeStatus(scope.row)" size="small" active-text="启用" inactive-text="禁用" />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="添加时间" width="150" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template #default="scope">
            <el-button v-if="!scope.row.isSuper" @click="editClick(scope.row)" type="text" size="small" v-auth="'/auth/user/edit'" icon="Edit">编辑</el-button>
            <el-button v-if="!scope.row.isSuper" @click="deleteClick(scope.row)" type="text" size="small" style="color:red;" v-auth="'/auth/user/delete'" icon="Delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <div class="pagination">
        <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
          :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"></el-pagination> -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="curPage" :page-sizes="[12, 24, 36, 42, 48]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalDataNumber"></el-pagination>
      </div>
    </el-card>
    <Add :open="open_add" :roleList="roleList" @close="closeAdd"></Add>
    <Edit :open="open_edit" :roleList="roleList" :model="curModel" @close="closeEdit"></Edit>
  </div>
</template>

<script>
import AuthApi from "@/api/auth.js";
import Add from "./dialog/Add.vue";
import Edit from "./dialog/Edit.vue";
import { useCascaderAreaData } from '@/utils/area'
import { ElMessage } from "element-plus";
import { defaultPageSize } from '@/utils/table.js'
export default {
  components: {
    Add,
    Edit,
  },
  inject: ["reload"],
  data() {
    return {
      formInline: {},
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      defaultData: useCascaderAreaData(),
      interDeparts: [
        { label: '生产部', value: 1 },
        { label: '仓库管理部', value: 2 },
        { label: '市场部', value: 3 },
        { label: '客服部', value: 4 },
        { label: '销售部', value: 5 },
        { label: '财务部', value: 6 },
        { label: '电商部', value: 7 },
        { label: '技术部', value: 8 }
      ],
      /*一页多少条*/
      pageSize: 12,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      curModel: {},
      /*角色列表*/
      roleList: [],
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    onSubmit() {
      this.getTableList();
    },
    reSet(){
      this.formInline = {};
      this.getTableList();
    },
    changeStatus(row) {
      const $this = this
      const { shopUserId, disabled, active } = row
      const dis = !active ? true : false
      AuthApi.changeUserStatus(shopUserId, dis, true)
        .then(result => {
          const { code, msg } = result
          if (code === 1) {
            ElMessage.success(msg)
            $this.getTableList();
          } else {
            ElMessage.error(msg)
          }
        }).catch(error => {

        })
      console.log('row', row)
    },
    renderOption(row) {
      const $this = this
      if (row && row.length > 0) {
        const htmls = []
        row.forEach(item => {
          // const province = $this.defaultData.find(f => f.value === item.province)
          // if (province) {
          //   const city = province.children.find(c => c.value === item.city)
          //   if (city) {
          //     const area = city.children.find(a => a.value === item.district)
          //     if (area) {
          // htmls.push(`<div>${province.label}/${city.label}/${area.label}</div>`)
          htmls.push(`<div>${item.areaName}</div>`)
          //     }
          //   }
          // }
        }
        )
        return htmls.join('')
      }
      return ''
      //defaultData
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = {};
      this.formInline['pageIndex'] = self.curPage;
      this.formInline['pageSize'] = self.pageSize;
      self.loading = true
      AuthApi.userList(this.formInline, true)
        .then((res) => {
          self.loading = false;
          self.tableData = res.data.userList.records;
          self.tableData.forEach(item => {
            item.active = item.disabled ? false : true
          })
          self.totalDataNumber = res.data.userList.total;
          self.roleList = res.data.roleList;
          self.loading = false
        })
        .catch((error) => {
          self.loading = false
        });
    },

    /*打开添加*/
    addClick() {
      this.open_add = true;
    },

    /*关闭添加*/
    closeAdd(e) {
      this.open_add = false;
      if (e && e.type == "success") {
        this.getTableList();
      }
    },

    /*打开编辑*/
    editClick(row) {
      this.curModel = row;
      this.open_edit = true;
    },

    /*关闭添加*/
    closeEdit(e) {
      this.open_edit = false;
      if (e && e.type == "success") {
        this.getTableList();
      }
    },

    /*删除*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          self.loading = true;
          AuthApi.userDelete(
            {
              shopUserId: row.shopUserId,
            },
            true
          )
            .then((data) => {
              self.loading = false;
              if (data.code == 1) {
                ElMessage({
                  message: "恭喜你，该管理员删除成功",
                  type: "success",
                });
                //刷新页面
                self.getTableList();
              } else {
                self.loading = false;
              }
            })
            .catch((error) => {
              self.loading = false;
            });
        })
        .catch(() => { });
    },
  },
};
</script>

<style></style>
